﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace DataAccessLayer {
    public class KhachHangRepository : DataProvider {
        public DataTable getTermsAndRepresentatives() {
            String sql = "SELECT CongTy.MaCongTy, CongTy.DiaChiCongTy, CongTy.DienThoaiCongTy, NguoiDaiDien.CMND, NguoiDaiDien.HoTen, NguoiDaiDien.DiaChi " +
                         "FROM CongTy INNER JOIN NguoiDaiDien ON CongTy.MaNguoiDaiDien = NguoiDaiDien.MaNguoiDaiDien";
            return this.executeQueryDataTable(sql);
        }

        public DataTable getTermsAndRepresentatives(String termID) {
            String sql = "SELECT CongTy.MaCongTy, CongTy.DiaChiCongTy, CongTy.DienThoaiCongTy, NguoiDaiDien.CMND, NguoiDaiDien.HoTen, NguoiDaiDien.DiaChi " +
                         "FROM CongTy INNER JOIN NguoiDaiDien ON CongTy.MaNguoiDaiDien = NguoiDaiDien.MaNguoiDaiDien "
                         + "WHERE CongTy.MaCongTy LIKE '%{0}%'";
            sql = String.Format(sql, termID);
            return this.executeQueryDataTable(sql);
        }

        public int insertContract(String contractType, float price, String employeeID, String termID) {

            String sql = "INSERT INTO HopDong(LoaiHopDong, GiaThanh, MaNhanVien, MaCongTy, NgayKy) VALUES('{0}', {1}, '{2}', '{3}', '{4}')";
            sql = string.Format(sql, contractType, price, employeeID, termID, DateTime.Now);
            if (this.executeQuery(sql) > 0) {
                sql = "SELECT MAX(MaHopDong) From HopDong";
                return int.Parse(this.executeScalar(sql).ToString());
            }
            else
                return -1;
        }
    }
}
